﻿Partial Public Class PageHost
    Inherits UserControl

    Dim _CurrentPage As UserControl
    Public ReadOnly Property CurrentPage As UserControl
        Get
            Return _CurrentPage
        End Get
    End Property

    Public Sub New(ByVal StartPage As UserControl)
        InitializeComponent()
        Me.Content = StartPage
        _CurrentPage = StartPage
    End Sub

    Public Sub Navigate(ByVal Page As UserControl)
        Dim FadeOutAnimation As Storyboard = DirectCast(Me.Resources("FadeOut"), Storyboard)
        FadeOutAnimation.Stop()
        Storyboard.SetTarget(FadeOutAnimation, Me.Content)
        AddHandler FadeOutAnimation.Completed, AddressOf FadeOutComplete
        FadeOutAnimation.Begin()
        _CurrentPage = Page
    End Sub

    Sub FadeOutComplete()
        Me.Content = _CurrentPage
        Dim FadeInAnimation As Storyboard = DirectCast(Me.Resources("FadeIn"), Storyboard)
        FadeInAnimation.Stop()
        Storyboard.SetTarget(FadeInAnimation, Me.Content)
        FadeInAnimation.Begin()
    End Sub
End Class
